/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package assignment2.pkg1;

import java.io.*;
/**
 *
 * @author MBoon
 */
public class SimResults {

    private double sumW; // sum of all the waiting times
    private int n;  // number of customers that left the queue so far
    private double[] waitingTimes; // array of all waiting times
    private double[] surface;
    private double lastT;
    private int MAXQUEUELENGTH = 1000;
    
    public SimResults( int numberOfCustomers ) {
        sumW = 0;
        n = 0;
        waitingTimes = new double[numberOfCustomers];
        surface = new double[MAXQUEUELENGTH];
        lastT = 0;
    }
    
    public void registerNumberOfCustomersInSystem(int nC, double t){
        if(nC >= MAXQUEUELENGTH)
            nC = MAXQUEUELENGTH-1;
        surface[nC] += (t-lastT);
        lastT = t;
    }
    
    public double[] getNumberOfCustomersProbabilities(){
        double[] prob = new double[surface.length];
        for(int i=0; i<surface.length; i++){
            prob[i] = surface[i]/lastT;
        }
        return prob;
    }
    
    public double getMeanNumberOfCustomersInSystem(){
        double[] prob = getNumberOfCustomersProbabilities();
        double mean = 0;
        for(int i=0;  i<prob.length; i++){
            mean += i*prob[i];
        }
        return mean;
    }
    
    public double[] getQueueLengthProbabilities(int nServers){
        double[] prob = new double[surface.length];
        for(int i=0; i<= nServers; i++){
            prob[0] += surface[i]/lastT;
        }
        for(int i=nServers+1; i<surface.length; i++){
            prob[i-nServers] = surface[i]/lastT;
        }
        return prob;
    }
    
    public void registerWaitingTime(double w) {
        sumW += w;
        waitingTimes[n] = w;
        n++;
    }
    
    public void printWaitingTimes(){
        for( int i = 0; i < waitingTimes.length; i++ ){
            System.out.println(waitingTimes[i]);
        }
    }
    
    public void export(String filename) throws IOException{
        File f = new File("D:/Documents/Assignment/"+filename+".txt");
        PrintWriter pw = new PrintWriter( new FileWriter(f) );
        
        for( int i = 0; i < waitingTimes.length; i++ ){
            pw.println(waitingTimes[i]);
        }
        
        pw.flush();
        pw.close();
    }
    
    public double getMeanWaitingTime() {
        return sumW / n;
    }
    
    
    
}
